package com.train.daily.mapper;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.train.common.model.page.PageSorted;
import com.train.common.resp.PageResp;
import com.train.daily.dto.token.DailyTrainSkTokenEditDTO;
import com.train.daily.dto.token.DailyTrainSkTokenPageDTO;
import com.train.daily.po.DailyTrainSkTokenPO;
import com.train.daily.resp.token.DailyTrainSkTokenPageResp;
import com.train.mybatis.base.MyBaseMapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【dailyTrainSkToken(秒杀令牌)】的数据库操作Mapper
 * @createDate 2025-03-20 00:19:41
 * @Entity generator.domain.DailyTrainSkToken
 */
public interface DailyTrainSkTokenMapper extends MyBaseMapper<DailyTrainSkTokenPO> {

    default PageResp<DailyTrainSkTokenPageResp> pageList(DailyTrainSkTokenPageDTO dailyTrainSkTokenPageDTO) {
        PageResp<DailyTrainSkTokenPO> dailyTrainSkTokenPOPageResp = this.selectPage(
                dailyTrainSkTokenPageDTO,
                new LambdaQueryWrapper<>(), new PageSorted(DailyTrainSkTokenPO::getUpdateTime), new PageSorted(DailyTrainSkTokenPO::getCreateTime));
        List<DailyTrainSkTokenPageResp> dailyTrainSkTokenPageRespList = BeanUtil.copyToList(dailyTrainSkTokenPOPageResp.getData(), DailyTrainSkTokenPageResp.class);
        return new PageResp<>(dailyTrainSkTokenPOPageResp.getTotal(), dailyTrainSkTokenPageRespList);
    }

    default boolean existsId(Long id) {
        LambdaQueryWrapper<DailyTrainSkTokenPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainSkTokenPO::getId, id);
        return this.exists(queryWrapper);
    }

    default boolean updateDailyTrainSkToken(DailyTrainSkTokenEditDTO dailyTrainSkTokenEditDTO) {
        LambdaUpdateWrapper<DailyTrainSkTokenPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DailyTrainSkTokenPO::getId, dailyTrainSkTokenEditDTO.getId())
                .set(DailyTrainSkTokenPO::getCount, dailyTrainSkTokenEditDTO.getCount());
        return this.update(updateWrapper) > 0;
    }

    default boolean existsTrains(LocalDate date, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainSkTokenPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainSkTokenPO::getDate, date)
                .in(DailyTrainSkTokenPO::getTrainCode, trainCodes);
        return this.exists(queryWrapper);
    }

    default boolean deleteTrainToken(LocalDate date, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainSkTokenPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainSkTokenPO::getDate, date)
                .in(DailyTrainSkTokenPO::getTrainCode, trainCodes);
        return this.delete(queryWrapper) > 0;
    }

    int decrementSkToken(@Param("trainCode") String trainCode,
                         @Param("date") LocalDate date,
                         @Param("seatType") String seatType,
                         @Param("decrementToken") Long decrementToken);

    default Long getSkToken(String trainCode, LocalDate date, String seatType) {
        LambdaQueryWrapper<DailyTrainSkTokenPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainSkTokenPO::getTrainCode, trainCode)
                .eq(DailyTrainSkTokenPO::getDate, date)
                .eq(DailyTrainSkTokenPO::getSeatType, seatType);
        DailyTrainSkTokenPO dailyTrainSkTokenPO = this.selectOne(queryWrapper);
        return dailyTrainSkTokenPO == null ? 0L : dailyTrainSkTokenPO.getCount();
    }
}